/**
 * jQuery分页插件，
 * 分页css效果基于bootstrap 2
 * @author zhaoYuan
 * @data 2014-9-18 19:12:15
 */

(function($) {

    // 参数选项设置...
    // 向jQuery原型中添加你的插件代码，用“pluginName”作为插件的函数名称。
    $.fn.pagination = function(options,pagination) {
        var settings = $.extend({}, $.fn.pagination.defaults, options || {});
        var page = $.extend({}, $.fn.pagination.defaultPagination, pagination || {});

        return this.each(function() {
            var self = $(this);
            self.empty();
            self.addClass(settings.paginationClass);

            var pane = $("<ul/>");

            var fp = $("<a/>");
            fp.html("&lt;&lt;");
            var pp = $("<a/>");
            pp.html("&lt;");
            var lp = $("<a/>");
            lp.html("&gt;&gt;");
            var np = $("<a/>");
            np.html("&gt;");

            var searchParams = page.queryString ? "&" + page.queryString : "";
            if(page.canGoPrevious){
                fp.attr("url-data","page=1" + searchParams);
                $("<li/>").append(fp).appendTo(pane);
                var ppNum = page.currentPage - 1;
                pp.attr("url-data", "page=" + ppNum + searchParams);
                $("<li/>").append(pp).appendTo(pane);
            }else{
                $("<li class='disabled'></li>").append(fp).appendTo(pane);
                $("<li class='disabled'></li>").append(pp).appendTo(pane);
            }

            var begin = Math.max(1, page.currentPage - settings.paginationSize / 2);
            var end = Math.min(begin + (settings.paginationSize - 1), page.pageCount);

            for(var i = begin; i <= end; i++){
                var p = $("<a/>");
                var li = $("<li/>");
                if(i == page.currentPage){
                    li.addClass("active");
                }
                p.html(i);
                p.attr("url-data", "page=" + i + searchParams);
                li.append(p).appendTo(pane);
            }

            if(page.canGoNext){
                var npNum = page.currentPage + 1;
                np.attr("url-data", "page=" + npNum + searchParams);
                $("<li/>").append(np).appendTo(pane);
                lp.attr("url-data","page=" + page.pageCount + searchParams);
                $("<li/>").append(lp).appendTo(pane);

            }else{
                $("<li class='disabled'></li>").append(np).appendTo(pane);
                $("<li class='disabled'></li>").append(lp).appendTo(pane);
            }
            self.append(pane);
            $("a",self).attr("href","javascript:void(0);");
            $("a",self).each(function(index, domEle){
                var data = {};
                data.url = page.url + "?" +  $(this).attr("url-data");
                data.self = self;
                data.target = settings.targetTable;
                data.settings = settings;
                $(this).click(data,settings.callBack);
            });

        });
    };
    $.fn.pagination.defaults = {
        paginationClass : "pagination",
        paginationSize  : 10,
        targetTable     : "",
        callBack        : function(){}
    }

    $.fn.pagination.defaultPagination = {
        begin: 5,
        canGoLast: false,
        canGoNext: false,
        canGoPrevious: true,
        count: 6,
        currentPage: 2,
        lastPage: true,
        pageCount: 2,
        pageSize: 5,
        previous: 1,
        queryString: "",
        url: "/view/mdin/${md.view.id}/"
    };
})(jQuery);